import React, { Component } from 'react'

export default class APP extends Component {
  constructor(props) {
    super(props)

    this.state = {
      counter: 0
    }
  }

  render() {
    return (
      <div>
        <h2>Current count: {this.state.counter}</h2>
        <button onClick={ e => this.increment() }>+1</button>
      </div>
    )
  }

  componentDidUpdate() {
    // 方式二：获取异步跟新的state
    console.log(this.state.counter)
  }

  increment() {
    // this.setState({
    //   counter: this.state.counter + 1
    // })
    // console.log(this.state.counter)

    // 方式一： 获取异步更新后的数据
    // setState(更新的state, 回调函数)
    this.setState({
      counter: this.state.counter + 1
    }, () => console.log(this.state.counter))
  }
}
